package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Calificaperiodo;
import com.lostreseditores.sabersai.model.dto.CalificaperiodoDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class CalificaperiodoView {
    private InputText txtAsigidn;
    private InputText txtCalixestuidn;
    private InputText txtPuntaje;
    private InputText txtCalifiperiodoidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<CalificaperiodoDTO> data;
    private CalificaperiodoDTO selectedCalificaperiodo;


    public CalificaperiodoView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            CalificaperiodoDTO calificaperiodoDTO = (CalificaperiodoDTO) e.getObject();

            if (txtAsigidn == null) {
                txtAsigidn = new InputText();
            }

            txtAsigidn.setValue(calificaperiodoDTO.getAsigidn());

            if (txtCalixestuidn == null) {
                txtCalixestuidn = new InputText();
            }

            txtCalixestuidn.setValue(calificaperiodoDTO.getCalixestuidn());

            if (txtPuntaje == null) {
                txtPuntaje = new InputText();
            }

            txtPuntaje.setValue(calificaperiodoDTO.getPuntaje());

            if (txtCalifiperiodoidn == null) {
                txtCalifiperiodoidn = new InputText();
            }

            txtCalifiperiodoidn.setValue(calificaperiodoDTO.getCalifiperiodoidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtAsigidn != null) {
            txtAsigidn.setValue(null);
            txtAsigidn.setDisabled(true);
        }

        if (txtCalixestuidn != null) {
            txtCalixestuidn.setValue(null);
            txtCalixestuidn.setDisabled(true);
        }

        if (txtPuntaje != null) {
            txtPuntaje.setValue(null);
            txtPuntaje.setDisabled(true);
        }

        if (txtCalifiperiodoidn != null) {
            txtCalifiperiodoidn.setValue(null);
            txtCalifiperiodoidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Calificaperiodo entity = null;

        try {
            Integer califiperiodoidn = new Integer(txtCalifiperiodoidn.getValue()
                                                                      .toString());
            entity = BusinessDelegatorView.getCalificaperiodo(califiperiodoidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtAsigidn.setDisabled(false);
            txtCalixestuidn.setDisabled(false);
            txtPuntaje.setDisabled(false);
            txtCalifiperiodoidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtAsigidn.setValue(entity.getAsigidn());
            txtAsigidn.setDisabled(false);
            txtCalixestuidn.setValue(entity.getCalixestuidn());
            txtCalixestuidn.setDisabled(false);
            txtPuntaje.setValue(entity.getPuntaje());
            txtPuntaje.setDisabled(false);
            txtCalifiperiodoidn.setValue(entity.getCalifiperiodoidn());
            txtCalifiperiodoidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveCalificaperiodo(FacesUtils.checkInteger(
                    txtAsigidn), FacesUtils.checkInteger(txtCalifiperiodoidn),
                FacesUtils.checkInteger(txtCalixestuidn),
                FacesUtils.checkLong(txtPuntaje));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteCalificaperiodo(FacesUtils.checkInteger(
                    txtCalifiperiodoidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateCalificaperiodo(FacesUtils.checkInteger(
                    txtAsigidn), FacesUtils.checkInteger(txtCalifiperiodoidn),
                FacesUtils.checkInteger(txtCalixestuidn),
                FacesUtils.checkLong(txtPuntaje));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtCalifiperiodoidn == null) {
                txtCalifiperiodoidn = new InputText();
            }

            txtCalifiperiodoidn.setValue(selectedCalificaperiodo.getCalifiperiodoidn());

            BusinessDelegatorView.deleteCalificaperiodo(FacesUtils.checkInteger(
                    txtCalifiperiodoidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedCalificaperiodo);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer asigidn,
        Integer califiperiodoidn, Integer calixestuidn, Long puntaje)
        throws Exception {
        try {
            BusinessDelegatorView.updateCalificaperiodo(asigidn,
                califiperiodoidn, calixestuidn, puntaje);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("CalificaperiodoView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtAsigidn() {
        return txtAsigidn;
    }

    public void setTxtAsigidn(InputText txtAsigidn) {
        this.txtAsigidn = txtAsigidn;
    }

    public InputText getTxtCalixestuidn() {
        return txtCalixestuidn;
    }

    public void setTxtCalixestuidn(InputText txtCalixestuidn) {
        this.txtCalixestuidn = txtCalixestuidn;
    }

    public InputText getTxtPuntaje() {
        return txtPuntaje;
    }

    public void setTxtPuntaje(InputText txtPuntaje) {
        this.txtPuntaje = txtPuntaje;
    }

    public InputText getTxtCalifiperiodoidn() {
        return txtCalifiperiodoidn;
    }

    public void setTxtCalifiperiodoidn(InputText txtCalifiperiodoidn) {
        this.txtCalifiperiodoidn = txtCalifiperiodoidn;
    }

    public List<CalificaperiodoDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataCalificaperiodo();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<CalificaperiodoDTO> calificaperiodoDTO) {
        this.data = calificaperiodoDTO;
    }

    public CalificaperiodoDTO getSelectedCalificaperiodo() {
        return selectedCalificaperiodo;
    }

    public void setSelectedCalificaperiodo(CalificaperiodoDTO calificaperiodo) {
        this.selectedCalificaperiodo = calificaperiodo;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

    
    
}
